package cn.edu.ujn.lab3.service;

import cn.edu.ujn.lab3.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.edu.ujn.lab3.dao.User;
import cn.edu.ujn.lab3.dao.UserMapper;

import java.util.List;

/**
 * 用户Service接口实现类
 */
@Service("userService")
@Transactional
public class UserServiceImpl implements UserService {
	// 注入UserDao
	@Autowired
	private UserMapper userDao;
	
	// 通过账号和密码查询用户
	@Override
	public User findUser(String usercode, String password) {
		// TODO Auto-generated method stub
		User user = this.userDao.findUser(usercode, password);
        return user;
	}

    @Override
    public Page<User> findUserList(Integer page, Integer rows, String userCode, String userName) {
        User user = new User();
        user.setUser_code(userCode);
        user.setUser_name(userName);

        // 当前页
        user.setStart((page-1) * rows) ;
        // 每页数
        user.setRows(rows);
        // 查询客户列表
        List<User> userList =
                userDao.selectUserList(user);
        // 查询客户列表总记录数
        Integer count = userDao.selectUserListCount(user);
        // 创建Page返回对象
        Page<User> result = new Page<>();
        result.setPage(page);
        result.setRows(userList);
        result.setSize(rows);
        result.setTotal(count);
        return result;
    }

    @Override
    public List<User> selectList(User user) {
        return userDao.selectUserList(user);
    }

    @Override
    public int createUser(User user) {
        return userDao.insert(user);
    }

    @Override
    public int updateUser(User user) {
        return userDao.updateByPrimaryKey(user);
    }

    @Override
    public int deleteUser(Integer id) {
        return userDao.deleteByPrimaryKey(id);
    }

    @Override
    public User getUserById(Integer id) {
        return userDao.selectByPrimaryKey(id);
    }

}
